package com.xueyu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xueyu.dao.OrdersDao;
import com.xueyu.dao.ProductDao;
import com.xueyu.domain.drug.drug;
import com.xueyu.domain.goods.*;
import com.xueyu.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/Orders")
public class OrdersContoller {
    @Autowired
    OrdersDao OrdersDao;
    @Autowired
    ProductDao ProductDao;

    @RequestMapping("/get")
    public ResultBean get(@RequestBody OrderRequest request) {
        Orders Ord = new Orders();
        List<Map<String, String>> ordList = request.getOrd();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String currentDate = dateFormat.format(new Date());
        Random random = new Random();
        int randomNum = random.nextInt(900000000) + 1000000000; // 生成10位随机数
        String orderNumber = currentDate + randomNum; //订单号
        for (Map<String, String> ord : ordList) {
            String drug = ord.get("drug");
            Ord.setProdid(Integer.valueOf(drug));
            String quantity = ord.get("quantity");
            Ord.setSytle(String.valueOf(0));
            Ord.setOrderdetailid(0);
            Ord.setOrderno(orderNumber);//订单号
            QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("prodid", drug);
            List<Product> page = ProductDao.selectList(queryWrapper);
            if (page != null) {
                Product firstDrug = page.get(0);
                firstDrug.getStyle();//商品单位
                firstDrug.getPrice();//商品单价
                Ord.setUnitname( firstDrug.getStyle());//订单的商品单位
                Ord.setPrice(firstDrug.getPrice());//订单的商品价格
                Ord.setProdcount(Integer.parseInt(quantity));//数量
                double result =  firstDrug.getPrice() * Integer.parseInt(quantity);
                Ord.setTotalprice(result);//小计

                Ord.setOrdertype(1);
                Date date = new Date();
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String currentTime = sdf1.format(date);
               Ord.setOrderdate(currentTime);

                Ord.setTime(currentTime);


               Ord.setStorageid(1);
               Ord.setSupplierid(2);
               Ord.setChairman(String.valueOf(1));
               Ord.setRemark("备注");
               Ord.setMobile("111111111111111");
                OrdersDao.insert(Ord);
            }
        }
        return new ResultBean();
    }
}

